package cn.wikiflyer.beikang.act.user.setting;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.bean.UserBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.net.WKAsyncTaskPro;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 设置登录
 * 
 * @author qunlee_mr
 * 
 */
public class UserSettingLoginAct extends BaseActivity {

	@ViewInject(id = R.id.login_phone)
	private EditText phone;
	@ViewInject(id = R.id.login_pwd)
	private EditText pwd;
	@ViewInject(id = R.id.login_forgetpwd, click = "onClick")
	private Button forgetpwd;
	@ViewInject(id = R.id.login_signin, click = "onClick")
	private Button signin;

	@ViewInject(id = R.id.login_phone_delbtn, click = "onClick")
	private ImageButton phone_del;
	@ViewInject(id = R.id.login_keshi_btn, click = "onClick")
	private ImageButton keshi_btn;

	@ViewInject(id = R.id.loginbtn, click = "onClick")
	private Button loginbtn;

	@ViewInject(id = R.id.ico_phone)
	private ImageView ico_phone;

	@ViewInject(id = R.id.ico_key)
	private ImageView ico_key;

	private boolean ifPwdKs;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_setting_login);
	}

	@Override
	public void init() {
		phone.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View arg0, boolean arg1) {
				if (arg1) {
					ico_phone.setImageResource(R.drawable.ico_login_phone);
					ico_key.setImageResource(R.drawable.ico_login_key);
				}
			}
		});
		pwd.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View arg0, boolean arg1) {
				if (arg1) {
					ico_phone.setImageResource(R.drawable.ico_login_phone_gary);
					ico_key.setImageResource(R.drawable.ico_login_key_selected);
				}
			}
		});

	}

	@Override
	public void setData() {
		if (IApplication.isAutoLogin) {
			try {
				phone.setText("first");
				phone.setText("15994741241");
				phone.setText("13480942081");
				phone.setText("13206410890");

				pwd.setText("123456");
				pwd.setText("111111");

				phone.setSelection(phone.length());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.login_phone:// 点击手机号
			ico_phone.setImageResource(R.drawable.ico_login_phone);
			ico_key.setImageResource(R.drawable.ico_login_key);
			break;
		case R.id.login_pwd:// 点击密码
			ico_phone.setImageResource(R.drawable.ico_login_phone_gary);
			ico_key.setImageResource(R.drawable.ico_login_key_selected);
			break;
		case R.id.login_forgetpwd:
			startActivity(new Intent(ctx, UserSettingFindPwdAct.class));
			break;
		case R.id.login_signin:
			startActivity(new Intent(ctx, UserSettingRegisterAct.class));
			break;
		case R.id.loginbtn:
			loginAction();
			break;
		case R.id.login_phone_delbtn:
			phone.setText("");
			break;
		case R.id.login_keshi_btn:
			if (ifPwdKs) {
				ifPwdKs = false;
				keshi_btn.setImageResource(R.drawable.login_xian_unselected);
				pwd.setInputType(InputType.TYPE_CLASS_TEXT
						| InputType.TYPE_TEXT_VARIATION_PASSWORD);
			} else {
				ifPwdKs = true;
				keshi_btn.setImageResource(R.drawable.login_xian_selected);
				pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			}
			pwd.setSelection(pwd.getText().toString().length());
			// pwd.setText("");

			break;
		default:
			break;
		}
	}

	private String tel;
	private String pass;

	// 登录
	private void loginAction() {

		tel = phone.getText().toString().trim();
		pass = pwd.getText().toString();

		if (TextUtils.isEmpty(phone.getText())) {
			WKApplication.showToast("请输入账号");
			return;
		}
		if (TextUtils.isEmpty(pwd.getText())) {
			WKApplication.showToast("请输入密码");
			return;
		}
		new GetLoginTask(ctx);
	}

	/**********************************************************************
	 * 数据接口
	 ***********************************************************************/
	/**
	 * 登录
	 */
	class GetLoginTask extends WKAsyncTaskPro {
		public GetLoginTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			return app().net.login("" + tel, "" + pass);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			try {

				Type cvbType = new TypeToken<BKBaseBean<UserBean>>() {
				}.getType();
				Gson gson = new Gson();
				BKBaseBean<UserBean> base = gson.fromJson(result, cvbType);
				if (base.status == 1) {
					app().setLoginSession(result);
					Toast.makeText(ctx, "登录成功", Toast.LENGTH_LONG).show();
					activity.finish();
					app().settingAct.finish();
				} else {
					app().sp.setString("set-cookie", "");
					Toast.makeText(ctx, "登录失败," + base.info, Toast.LENGTH_LONG)
							.show();
				}

			} catch (Exception e) {
				e.printStackTrace();
				Toast.makeText(ctx, "登录失败", Toast.LENGTH_LONG).show();
			}
		}
	}

}